<?php

namespace app\task\cron\coupon;

use app\event\OrderPayRefund;
use app\listener\base\CouponOrderUnion;
use app\model\CouponOrder;
use yunwuxin\cron\Task;

class RefundOrder extends Task
{
    /**
     * 订单退款
     */
    protected function configure()
    {
        // 分 时 日 月 周
        $this->expression = '* * * * *';

    }

    /**
     * 执行指令.
     */
    protected function handle()
    {

        echo '统一订单退款-开始' . PHP_EOL;
        $lockKey = 'refund_order_cron';
        $lockVal = cache($lockKey);

        if ($lockVal) {
            echo '还在处理呢' . PHP_EOL;
            return true;
        }
        cache($lockKey, 1, 60);

        //保险一点 只退款自营的
        CouponOrder::query()
            ->where('type', CouponOrder::TYPE_COUPON_SELF_EMPLOYED)
//            ->whereIn('serv_type', $this->productServMap)
            ->where('status', CouponOrder::C_STATUS_RECHARGE_FAIL)
            ->where('supplier_status', CouponOrder::SUPPLIER_STATUS_FAIL)
            ->where('refund_status', CouponOrder::C_REFUND_STATUS_ING)
            ->select()->each(function (CouponOrder $couponOrder) {
                event(new OrderPayRefund($couponOrder));
            });

        cache($lockKey, null);
        echo '订单退款结束'.PHP_EOL;
    }

}